package com.cabrida.livestar.mainstar;

import java.util.ArrayList;

import a.a.a.a.a.e;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.model.StarCircleData;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;

/**
 * 明星端星友圈adapter
 * 
 * @author why
 * 
 */
public class StarCircleLvAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<StarCircleData> data_list = new ArrayList<StarCircleData>();

	public StarCircleLvAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
	}

	public void setData(ArrayList<StarCircleData> data) {
		data_list = data;
	}

	@Override
	public int getCount() {
		return data_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data_list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(
					R.layout.adapter_fans_starcircle_lv_item, null);
			holder.ll_onlive = (LinearLayout) convertView.findViewById(R.id.ll_onlive);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
			holder.tv_like_num = (TextView) convertView.findViewById(R.id.tv_like_num);
			holder.tv_comment_num = (TextView) convertView.findViewById(R.id.tv_comment_num);
			holder.tv_share_num = (TextView) convertView.findViewById(R.id.tv_share_num);
			holder.ll_image_1 = (LinearLayout) convertView.findViewById(R.id.ll_image_1);
			holder.ll_image_2 = (LinearLayout) convertView.findViewById(R.id.ll_image_2);
			holder.ll_image_3 = (LinearLayout) convertView.findViewById(R.id.ll_image_3);
			holder.image_1 = (ImageView) convertView.findViewById(R.id.image_1);
			holder.image_2 = (ImageView) convertView.findViewById(R.id.image_2);
			holder.image_3= (ImageView) convertView.findViewById(R.id.image_3);
			holder.image_4 = (ImageView) convertView.findViewById(R.id.image_4);
			holder.image_5 = (ImageView) convertView.findViewById(R.id.image_5);
			holder.image_6 = (ImageView) convertView.findViewById(R.id.image_6);
			holder.image_7 = (ImageView) convertView.findViewById(R.id.image_7);
			holder.image_8 = (ImageView) convertView.findViewById(R.id.image_8);
			holder.image_9 = (ImageView) convertView.findViewById(R.id.image_9);
			holder.iv_header = (CircleImageView) convertView.findViewById(R.id.iv_header);
			holder.ll_like = (LinearLayout) convertView.findViewById(R.id.ll_like);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		
		StarCircleData data = data_list.get(position);
		
		MyUtils.loadImg(data.getU_avatar(), holder.iv_header);
		holder.tv_name.setText(data.getU_nickname().toString());
		holder.tv_time.setText(data.getC_createtime().toString());
		if (!TextUtils.isEmpty(data.getC_content())) {
			holder.tv_content.setText(data.getC_content().toString());
		}else {
			holder.tv_content.setText("");
		}
		holder.tv_like_num.setText(data.getC_like().toString());
		holder.tv_comment_num.setText(data.getC_comment().toString());
		holder.tv_share_num.setText(data.getC_share().toString());
		holder.ll_image_1.setVisibility(View.GONE);
		holder.ll_image_2.setVisibility(View.GONE);
		holder.ll_image_3.setVisibility(View.GONE);
		if (!TextUtils.isEmpty(data.getC_images())) {
			String strs[] = data.getC_images().split(",");
			switch (strs.length) {
			case 1:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.GONE);
				holder.ll_image_3.setVisibility(View.GONE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.INVISIBLE);
				holder.image_3.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				break;
			case 2:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.GONE);
				holder.ll_image_3.setVisibility(View.GONE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				break;
			case 3:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.GONE);
				holder.ll_image_3.setVisibility(View.GONE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.VISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				MyUtils.loadImg(strs[2], holder.image_3);
				break;
			case 4:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.VISIBLE);
				holder.ll_image_3.setVisibility(View.GONE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.INVISIBLE);
				holder.image_4.setVisibility(View.VISIBLE);
				holder.image_5.setVisibility(View.VISIBLE);
				holder.image_6.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				MyUtils.loadImg(strs[2], holder.image_4);
				MyUtils.loadImg(strs[3], holder.image_5);
				break;
			case 5:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.VISIBLE);
				holder.ll_image_3.setVisibility(View.GONE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.VISIBLE);
				holder.image_4.setVisibility(View.VISIBLE);
				holder.image_5.setVisibility(View.VISIBLE);
				holder.image_6.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				MyUtils.loadImg(strs[2], holder.image_3);
				MyUtils.loadImg(strs[3], holder.image_4);
				MyUtils.loadImg(strs[4], holder.image_5);
				break;
			case 6:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.VISIBLE);
				holder.ll_image_3.setVisibility(View.GONE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.VISIBLE);
				holder.image_4.setVisibility(View.VISIBLE);
				holder.image_5.setVisibility(View.VISIBLE);
				holder.image_6.setVisibility(View.VISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				MyUtils.loadImg(strs[2], holder.image_3);
				MyUtils.loadImg(strs[3], holder.image_4);
				MyUtils.loadImg(strs[4], holder.image_5);
				MyUtils.loadImg(strs[5], holder.image_6);
				break;
			case 7:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.VISIBLE);
				holder.ll_image_3.setVisibility(View.VISIBLE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.VISIBLE);
				holder.image_4.setVisibility(View.VISIBLE);
				holder.image_5.setVisibility(View.VISIBLE);
				holder.image_6.setVisibility(View.VISIBLE);
				holder.image_7.setVisibility(View.VISIBLE);
				holder.image_8.setVisibility(View.INVISIBLE);
				holder.image_9.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				MyUtils.loadImg(strs[2], holder.image_3);
				MyUtils.loadImg(strs[3], holder.image_4);
				MyUtils.loadImg(strs[4], holder.image_5);
				MyUtils.loadImg(strs[5], holder.image_6);
				MyUtils.loadImg(strs[6], holder.image_7);
				break;
			case 8:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.VISIBLE);
				holder.ll_image_3.setVisibility(View.VISIBLE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.VISIBLE);
				holder.image_4.setVisibility(View.VISIBLE);
				holder.image_5.setVisibility(View.VISIBLE);
				holder.image_6.setVisibility(View.VISIBLE);
				holder.image_7.setVisibility(View.VISIBLE);
				holder.image_8.setVisibility(View.VISIBLE);
				holder.image_9.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				MyUtils.loadImg(strs[2], holder.image_3);
				MyUtils.loadImg(strs[3], holder.image_4);
				MyUtils.loadImg(strs[4], holder.image_5);
				MyUtils.loadImg(strs[5], holder.image_6);
				MyUtils.loadImg(strs[6], holder.image_7);
				MyUtils.loadImg(strs[7], holder.image_8);
				break;
			case 9:
				holder.ll_image_1.setVisibility(View.VISIBLE);
				holder.ll_image_2.setVisibility(View.VISIBLE);
				holder.ll_image_3.setVisibility(View.VISIBLE);
				holder.image_1.setVisibility(View.VISIBLE);
				holder.image_2.setVisibility(View.VISIBLE);
				holder.image_3.setVisibility(View.VISIBLE);
				holder.image_4.setVisibility(View.VISIBLE);
				holder.image_5.setVisibility(View.VISIBLE);
				holder.image_6.setVisibility(View.VISIBLE);
				holder.image_7.setVisibility(View.VISIBLE);
				holder.image_8.setVisibility(View.VISIBLE);
				holder.image_9.setVisibility(View.VISIBLE);
				MyUtils.loadImg(strs[0], holder.image_1);
				MyUtils.loadImg(strs[1], holder.image_2);
				MyUtils.loadImg(strs[2], holder.image_3);
				MyUtils.loadImg(strs[3], holder.image_4);
				MyUtils.loadImg(strs[4], holder.image_5);
				MyUtils.loadImg(strs[5], holder.image_6);
				MyUtils.loadImg(strs[6], holder.image_7);
				MyUtils.loadImg(strs[7], holder.image_8);
				MyUtils.loadImg(strs[8], holder.image_9);
				break;

			default:
				break;
			}
		}
		holder.ll_like.setTag(position);
		holder.ll_like.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int pos = (Integer) v.getTag();
				mistener.onLike(data_list.get(pos).getCid());
			}
		});
		return convertView;
	}
	
	private CircleLikeListener mistener;

	public interface CircleLikeListener {
		public void onLike(String cid);
	}

	public void setOnCircleLikeListener(CircleLikeListener listener) {
		mistener = listener;
	}

	class ViewHolder {
		private LinearLayout ll_onlive;
		private TextView tv_name;
		private TextView tv_time;
		private TextView tv_content;
		private TextView tv_like_num;
		private TextView tv_comment_num;
		private TextView tv_share_num;
		private LinearLayout ll_image_1;
		private LinearLayout ll_image_2;
		private LinearLayout ll_image_3;
		private ImageView image_1;
		private ImageView image_2;
		private ImageView image_3;
		private ImageView image_4;
		private ImageView image_5;
		private ImageView image_6;
		private ImageView image_7;
		private ImageView image_8;
		private ImageView image_9;
		private CircleImageView iv_header;
		private LinearLayout ll_like;
	}

}
